import request from '@/utils/request'
import type { Course } from './course'

// 资源类型定义
export interface Resource {
  id: number
  title: string
  description: string
  type: 'book' | 'video' | 'article' | 'project' | 'course'
  category: string
  difficulty: 'beginner' | 'medium' | 'advanced'
  author: string
  author_id?: number
  views: number
  likes: number
  status: 'draft' | 'published' | 'archived'
  created_at: string
  updated_at: string
  
  // 通用字段
  url?: string
  thumbnail_url?: string
  file_size?: number
  duration?: number
  
  // 书籍特有字段
  isbn?: string
  publisher?: string
  pages?: number
  language?: string
  
  // 视频特有字段
  video_url?: string
  video_source?: string
  video_id?: string
  
  // 项目特有字段
  tech_stack?: string[]
  github_url?: string
  demo_url?: string
  cover_image?: string
  images?: string[]
  
  // 文章特有字段
  content?: string
  word_count?: number
  
  // 课程特有字段
  rating?: number
  rating_count?: number
  
  // 关联数据
  tags?: Tag[]
  author_user?: User
  course?: Course
}

export interface Tag {
  id: number
  name: string
  description?: string
  color?: string
}

export interface User {
  id: number
  username: string
  email: string
  avatar?: string
  role: string
}

export interface ResourceComment {
  id: number
  user_id: number
  resource_id: number
  content: string
  rating?: number
  likes: number
  created_at: string
  updated_at: string
  user: User
}

// 请求参数接口
export interface GetResourcesParams {
  page?: number
  page_size?: number
  category?: string
  type?: string
  difficulty?: string
  search?: string
  sort_by?: string
  sort_order?: 'asc' | 'desc'
}

export interface CreateResourceRequest {
  title: string
  description: string
  type: 'book' | 'video' | 'article' | 'project' | 'course'
  category: string
  difficulty: 'beginner' | 'medium' | 'advanced'
  author: string
  
  // 可选字段
  url?: string
  thumbnail_url?: string
  file_size?: number
  duration?: number
  
  // 书籍特有字段
  isbn?: string
  publisher?: string
  pages?: number
  language?: string
  
  // 视频特有字段
  video_url?: string
  video_source?: string
  video_id?: string
  
  // 项目特有字段
  tech_stack?: string[]
  github_url?: string
  demo_url?: string
  cover_image?: string
  images?: string[]
  
  // 文章特有字段
  content?: string
  word_count?: number
  
  // 标签
  tag_ids?: number[]
}

export interface UpdateResourceRequest {
  title?: string
  description?: string
  category?: string
  difficulty?: 'beginner' | 'medium' | 'advanced'
  author?: string
  status?: 'draft' | 'published' | 'archived'
  
  // 可选字段
  url?: string
  thumbnail_url?: string
  file_size?: number
  duration?: number
  
  // 书籍特有字段
  isbn?: string
  publisher?: string
  pages?: number
  language?: string
  
  // 视频特有字段
  video_url?: string
  video_source?: string
  video_id?: string
  
  // 项目特有字段
  tech_stack?: string[]
  github_url?: string
  demo_url?: string
  cover_image?: string
  images?: string[]
  
  // 文章特有字段
  content?: string
  word_count?: number
  
  // 标签
  tag_ids?: number[]
}

export interface CreateResourceCommentRequest {
  content: string
  rating?: number
}

// 响应接口
export interface ResourceListResponse {
  items: Resource[]
  total: number
  page: number
  page_size: number
  total_pages: number
}

export interface ResourceCommentListResponse {
  items: ResourceComment[]
  total: number
  page: number
  page_size: number
  total_pages: number
}

// 资源API类
export class ResourceAPI {
  // 获取资源列表
  static async getResources(params: GetResourcesParams = {}): Promise<ResourceListResponse> {
    const response = await request.get('/v1/resources', { params })
    return response.data
  }

  // 获取资源详情
  static async getResource(id: number): Promise<Resource> {
    const response = await request.get(`/v1/resources/${id}`)
    return response.data
  }

  // 创建资源
  static async createResource(data: CreateResourceRequest): Promise<{ id: number }> {
    const response = await request.post('/v1/resources', data)
    return response.data
  }

  // 更新资源
  static async updateResource(id: number, data: UpdateResourceRequest): Promise<{ id: number }> {
    const response = await request.put(`/v1/resources/${id}`, data)
    return response.data
  }

  // 删除资源
  static async deleteResource(id: number): Promise<void> {
    await request.delete(`/v1/resources/${id}`)
  }

  // 点赞/取消点赞资源
  static async likeResource(id: number): Promise<{ liked: boolean; likes: number }> {
    const response = await request.post(`/v1/resources/${id}/like`)
    return response.data
  }

  // 获取资源评论
  static async getResourceComments(id: number, page: number = 1, pageSize: number = 10): Promise<ResourceCommentListResponse> {
    const response = await request.get(`/v1/resources/${id}/comments`, {
      params: { page, page_size: pageSize }
    })
    return response.data
  }

  // 创建资源评论
  static async createResourceComment(id: number, data: CreateResourceCommentRequest): Promise<ResourceComment> {
    const response = await request.post(`/v1/resources/${id}/comments`, data)
    return response.data
  }
}

// 导出默认实例
export const resourceApi = new ResourceAPI()
